package com.zw.pdm.module.report.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.rep.Sheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.ReportAddService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zw.pdm.constant.Constant.*;

/**
 * @Description: 创建计划接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/report/add")
@SuppressWarnings("all")
public class ReportAddController extends BaseController {

    @Autowired
    private SheetService sheetService;
    @Autowired
    private ReportAddService reportAddService;
    @Autowired
    private DictCommonService dictCommonService;

    @GetMapping("/out")
    @RequiresPermissions(value = {"report:out:view"}, logical = Logical.OR)
    public String outPage(Model model) {
        model.addAttribute("malTypes", dictCommonService.findDictByGroupName(GROUP_MALTYPE));
        return "report/add/outPage";
    }

    @GetMapping("/ass")
    @RequiresPermissions({"report:ass:view"})
    public String assPage(Model model) {
        model.addAttribute("assTypes", dictCommonService.findDictByGroupName(GROUP_ASSTYPE));
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "report/add/assPage";
    }

    @GetMapping("/mal")
    @RequiresPermissions(value = {"report:mal:view"},logical = Logical.OR)
    public String malPage(Model model) {
        model.addAttribute("specials", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("qualities", dictCommonService.findDictByGroupName(GROUP_QUALITY));
        return "report/add/malPage";
    }

    @GetMapping("/project/list")
    @ResponseBody
    public List<Project> proList(String q) {
        return sheetService.findProjectByCode(q);
    }

    /** 上传附件到临时文件夹 */
    @ResponseBody
    @PostMapping("/upload")
    @RequiresPermissions(value = {"report:out:edit", "report:ass:edit", "report:mal:edit"}, logical = Logical.OR)
    public JsonResult uploadExtends(@RequestParam("file") MultipartFile file) {
        String path = sheetService.uploadTemp(file);
        return success().setData(path);
    }

    /**
     * 保存外购件计划单
     */
    @ResponseBody
    @PostMapping("/out/save")
    @RequiresPermissions(value = {"report:out:edit"}, logical = Logical.OR)
    public JsonResult saveOut(Sheet sheet, String[] malDesc, Double[] num, String[] unit, String[] standard, String[] brand, String[] remark, String[] url) {
        return jsonResult(reportAddService.saveOut(sheet, malDesc, num, unit, standard, brand, remark, url));
    }

    /**
     * 保存外协件计划单
     */
    @ResponseBody
    @PostMapping("/ass/save")
    @RequiresPermissions({"report:ass:edit"})
    public JsonResult saveAss(Sheet sheet, String[] malDesc, String[] diagram, Double[] num, String[] unit, String[] assistType, String[] remark, String[] url) {
        return jsonResult(reportAddService.saveAss(sheet, malDesc, diagram, num, unit, assistType, remark, url));
    }

    /**
     * 保存原材料计划单
     */
    @ResponseBody
    @PostMapping("/mal/save")
    @RequiresPermissions(value = {"report:mal:edit"}, logical = Logical.OR)
    public JsonResult saveMal(Sheet sheet, String[] malDesc, Double[] num, String[] unit, String[] standard, String[] quality,  String[] useStore, String[] remark, String[] url) {
        return jsonResult(reportAddService.saveMal(sheet, malDesc, num, unit, standard, quality, useStore, remark, url));
    }

}
